package com.spring.service.content;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import com.spring.domain.ContentSkin;
import com.spring.domain.ContentType;

public class JdbcContentTypeDao extends SimpleJdbcDaoSupport implements ContentTypeDao {
	protected final Log logger = LogFactory.getLog(getClass());
	
	
	
	public List<ContentType> getContentTypes() {
		return getSimpleJdbcTemplate().query( "select id,descrizione,max_img_number,gallery from content_type", new ContentTypeMapper());
	}
	
	public List<ContentSkin> getContentSkins() {
		return getSimpleJdbcTemplate().query( "select id,id_content_skin,id_content_type,url_preview, url_preview, url_prod from content_skin", new ContentSkinMapper());
	}
	
	private static class ContentSkinMapper implements ParameterizedRowMapper<ContentSkin> {

        public ContentSkin mapRow(ResultSet rs, int rowNum) throws SQLException {
        	ContentSkin obj = new ContentSkin();
        	obj.setId(rs.getInt("id"));
        	obj.setIdContentSkin(rs.getInt("id_content_skin"));
        	obj.setIdContentType(rs.getInt("id_content_type"));
        	obj.setUrlPreview(rs.getString("url_preview"));
        	obj.setUrlProd(rs.getString("url_prod"));
            return obj;
        }
        
        public static MapSqlParameterSource sqlMap (ContentSkin obj){
        	return new MapSqlParameterSource()
    				.addValue("id", obj.getId())
    				.addValue("id_content_skin", obj.getIdContentSkin())
    				.addValue("id_content_type", obj.getIdContentType())
    				.addValue("url_preview", obj.getUrlPreview())
        			.addValue("url_prod", obj.getUrlProd());
        	
        }

 }
	
	private static class ContentTypeMapper implements ParameterizedRowMapper<ContentType> {

	        public ContentType mapRow(ResultSet rs, int rowNum) throws SQLException {
	        	ContentType contentType = new ContentType();
	        	contentType.setId(rs.getInt("id"));
	        	contentType.setDescrizione(rs.getString("descrizione"));
	        	contentType.setMax_img_number(rs.getInt("max_img_number"));
	        	contentType.setGallery(rs.getInt("gallery"));
	        
	            return contentType;
	        }
	        
	        public static MapSqlParameterSource sqlMap (ContentType contentType){
	        	return new MapSqlParameterSource()
	    				.addValue("id", contentType.getId())
	    				.addValue("descrizione", contentType.getDescrizione())
	    				.addValue("max_img_number", contentType.getMax_img_number())
	    				.addValue("gallery", contentType.getGallery());
	        		
	        	
	        }

	 }
	
}
